//导入需要的模块
const http = require('http')
const path = require('path')
const getinfo = require('./getInfo.js')
const fs = require('fs')
// 创建 web 服务器实例
const server = http.createServer()
// 启动服务器
server.listen(8848, () => {
  console.log('my server start work')
})
//为服务器实例绑定 request 事件，监听客户端的请求
server.on('request', (request, response) => {
 const url = request.url
 response.setHeader('Content-Type', 'application/json; charset=utf-8')
 console.log(url);
 //获取新闻列表
 if(url==='/api/news'){
    response.end(JSON.stringify(getinfo.news()))
 }
//获取学生列表
 else if(url==='/api/students'){
    response.end(JSON.stringify(getinfo.students()))
 }
 //获取幸运儿
 else if(url==='/api/luckystar'){
    response.end(JSON.stringify(getinfo.luckystar().name) )
 }
 //处理错误url
 else{
   response.end("404 NotFound")
 }
})

